package com.nbicc.ad.service.impl;

import com.github.pagehelper.Page;
import com.nbicc.ad.mapper.AdInstanceMapper;
import com.nbicc.ad.po.AdInstance;
import com.nbicc.ad.service.IAdInstanceService;
import com.nbicc.ad.vo.AdInstanceQueryCondition;
import com.nbicc.common.Const;
import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.news.po.NewsInfo;
import com.nbicc.news.service.INewsInfoService;
import com.nbicc.news.service.INewsService;
import com.nbicc.parent.serivce.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;

/**
 * @Author: lilingyan
 * @Date 2018/8/2 10:38
 */
@Service("AdInstanceService")
public class AdInstanceService extends BaseService implements IAdInstanceService {

    @Autowired
    private AdInstanceMapper adInstanceMapper;
    @Resource(name = "NewsInfoServiceImpl")
    private INewsInfoService newsInfoService;
    @Resource(name = "NewsServiceImpl")
    private INewsService newsService;

    @Override
    public Integer insert(AdInstance adInstance) throws Exception {
        adInstance.setCreateTime(new Date());
        return adInstanceMapper.insert(adInstance);
    }

    @Override
    public AdInstance selectById(Number id) throws Exception {
        return adInstanceMapper.selectById(id);
    }

    @Override
    public Integer updateById(Object adInstance) throws Exception {
        return adInstanceMapper.updateById(adInstance);
    }

    @Override
    public Integer deleteById(Number id) {
        return adInstanceMapper.deleteById(id);
    }

    @Override
    public ResponseObj find(RequestPageObj<Map<String, Object>> requestPageObj) {
        setPageHelper(requestPageObj , AdInstance.TABLE_NEWS_INFO);
        List<AdInstance> adInstanceList = adInstanceMapper.selectByMap(requestPageObj.getData());
        return new ResponseObj(Const.HTTP_STATUS_SUCCESS, adInstanceList, ((Page<?>) adInstanceList).getTotal());
    }

    @Override
    public List<AdInstance> selectListByAdInstanceQueryCondition(AdInstanceQueryCondition adInstanceQueryCondition) {
        List<AdInstance> adInstanceList = adInstanceMapper.selectListByAdInstanceQueryCondition(adInstanceQueryCondition);
        return adInstanceList;
    }

    /**
     * 把新闻简介设置到广告中
     * @param adInstanceList
     */
    public void applyNewsInfoToAdInstanceList(List<AdInstance> adInstanceList){
        Set<Serializable> idSet = new LinkedHashSet<>();
        for (AdInstance adInstance : adInstanceList) {
            idSet.add(adInstance.getTargetUrl());
        }
        List<NewsInfo> newsInfos = newsInfoService.selectBatchIds(idSet);
        if(!CollectionUtils.isEmpty(newsInfos)){

            for (NewsInfo newsInfo : newsInfos) {
                newsService.applyVisitCountToNews(newsInfo,newsInfo.getId());
            }

            adInstanceList.stream().forEach(
                    adInstance ->
                            newsInfos.stream().forEach(
                                    newsInfo -> {
                                        if(!StringUtils.isEmpty(adInstance.getTargetUrl()) && adInstance.getTargetUrl().equals(String.valueOf(newsInfo.getId()))){
                                            adInstance.setNewsInfo(newsInfo);
                                        }
                                    }
                            )
            );
        }
    }

}
